package org.example.service.post.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.dao.post.*;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.commom.UserInfo;
import org.example.dao.dto.post.PCommentDto;
import org.example.partten.builder.product.post.PPostDto;
import org.example.dao.dto.post.PReplyDto;
import org.example.dao.dto.post.ReportDto;
import org.example.dao.entity.post.*;
import org.example.dao.entity.user.UUser;
import org.example.exception.ParamLossErrorException;
import org.example.dao.entity.post.PImage;
import org.example.service.post.ReportService;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Service("reportService")
public class ReportServiceImpl implements ReportService {

    @Resource
    private PReportDao pReportDao;

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private IdWorker idWorker;

    @Resource
    private PPostDao postDao;

    @Resource
    private PCommentDao commentDao;

    @Resource
    private PReplyDao pReplyDao;

    @Resource
    private PImageDao pImageDao;

    @Resource
    private UUserDao uUserDao;

    @Override
    public void deleteReport(Long id) {
        PReport pReport = pReportDao.selectById(id);
        pReport.setUpdateTime(new Date());
        pReport.setIsValid(0);
        pReportDao.updateById(pReport);
    }

    @Override
    public String updateReportStatus(Long id, String status) {
        PReport pReport = pReportDao.selectById(id);
        pReport.setUpdateTime(new Date());
        pReport.setStatus(status);
        pReportDao.updateById(pReport);
        return status;
    }

    @Override
    public List<String> selectTypeList() {
        List<String> list = new ArrayList<>();
        list.add("广告");
        list.add("人身攻击");
        list.add("色情");
        list.add("政治有害");
        list.add("涉嫌欺诈");
        list.add("其他");
        return list;
    }

    @Override
    public IPage<ReportDto> selectReportByTypeAndStatus(String type, String status, String kind, Long limit, Long start) {
        LambdaQueryWrapper<PReport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PReport::getType, type);
        lambdaQueryWrapper.eq(PReport::getStatus, status);
        lambdaQueryWrapper.eq(PReport::getIsValid, 1);
        lambdaQueryWrapper.eq(PReport::getKind, kind);
        lambdaQueryWrapper.orderBy(true, false, PReport::getCreateTime);
        IPage<PReport> iPage = pReportDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        List<ReportDto> list = new ArrayList<>();

        for (PReport report : iPage.getRecords()) {

            Long userId = report.getUserId();
            Long linkId = report.getLinkId();
            UUser sender = uUserDao.selectById(userId);

            if ("post".equals(kind)) {

                PPost post = postDao.selectById(linkId);
                if(post==null){
                    throw new ParamLossErrorException("linkid传递错误，不存在");
                }

                PPostDto postDto = new PPostDto();
                postDto.setPPost(post);
                UUser receiver = uUserDao.selectById(post.getUserId());
                postDto.setUserInfo(new UserInfo(receiver));

                LambdaQueryWrapper<PImage> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(PImage::getPostId, post.getId());
                lambdaQueryWrapper1.eq(PImage::getIsValid, 1);
                List<PImage> list1 = pImageDao.selectList(lambdaQueryWrapper1);
                postDto.setImageList(list1);

                ReportDto reportDto = new ReportDto();
                reportDto.setReport(report);
                reportDto.setReceiver(new UserInfo(receiver));
                reportDto.setSender(new UserInfo(sender));
                reportDto.setPost(postDto);
                list.add(reportDto);
            } else if ("comment".equals(kind)) {
                PComment comment = commentDao.selectById(linkId);
                if(comment==null){
                    throw new ParamLossErrorException("linkid传递错误，不存在");
                }

                UUser receiver = uUserDao.selectById(comment.getUserId());
                PCommentDto commentDto = new PCommentDto();
                commentDto.setPComment(comment);
                commentDto.setUserInfo(new UserInfo(receiver));

                ReportDto reportDto = new ReportDto();
                reportDto.setReport(report);
                reportDto.setReceiver(new UserInfo(receiver));
                reportDto.setSender(new UserInfo(sender));
                reportDto.setComment(commentDto);
                list.add(reportDto);
            } else if ("reply".equals(kind)) {
                PReply pReply = pReplyDao.selectById(linkId);
                if(pReply==null){
                    throw new ParamLossErrorException("linkid传递错误，不存在");
                }

                UUser receiver = uUserDao.selectById(pReply.getSenderId());
                PReplyDto pReplyDto = new PReplyDto();
                pReplyDto.setPReply(pReply);
                pReplyDto.setSenderUser(new UserInfo(receiver));
                pReplyDto.setReceiverUser(new UserInfo(uUserDao.selectById(pReply.getReceiverId())));
                ReportDto reportDto = new ReportDto();
                reportDto.setReport(report);
                reportDto.setReceiver(new UserInfo(receiver));
                reportDto.setSender(new UserInfo(sender));
                reportDto.setReply(pReplyDto);
                list.add(reportDto);
            } else {
                throw new ParamLossErrorException("kind传递值错误");
            }
        }
        IPage<ReportDto> iPage1 = new Page<>();
        iPage1.setRecords(list);
        iPage.setTotal(iPage.getTotal());
        iPage.setPages(iPage.getPages());
        iPage.setCurrent(iPage.getCurrent());
        iPage.setSize(iPage.getSize());
        return iPage1;
    }

    @Override
    public ReportDto addReport(String due, Long linkId, String type, String kind) {
        Long userId = contextUtil.getId();
        Long id = idWorker.nextId();
        PReport pReport = new PReport(id, userId, due, linkId, type,kind);
        pReportDao.insert(pReport);

        ReportDto reportDto = new ReportDto();
        UUser sender = uUserDao.selectById(userId);
        reportDto.setReport(pReport);
        reportDto.setSender(new UserInfo(sender));


        if ("post".equals(kind)) {
            PPost post = postDao.selectById(linkId);
            if(post==null){
                throw new ParamLossErrorException("linkid传递错误，不存在");
            }

            PPostDto postDto = new PPostDto();
            postDto.setPPost(post);
            UUser receiver = uUserDao.selectById(post.getUserId());
            postDto.setUserInfo(new UserInfo(receiver));
            LambdaQueryWrapper<PImage> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(PImage::getPostId, post.getId());
            lambdaQueryWrapper1.eq(PImage::getIsValid, 1);
            List<PImage> list1 = pImageDao.selectList(lambdaQueryWrapper1);
            postDto.setImageList(list1);
            reportDto.setPost(postDto);
            reportDto.setReceiver(new UserInfo(receiver));
        } else if ("comment".equals(kind)) {
            PComment comment = commentDao.selectById(linkId);
            if(comment==null){
                throw new ParamLossErrorException("linkid传递错误，不存在");
            }

            UUser receiver = uUserDao.selectById(comment.getUserId());
            PCommentDto commentDto = new PCommentDto();
            commentDto.setPComment(comment);
            commentDto.setUserInfo(new UserInfo(receiver));
            reportDto.setReceiver(new UserInfo(receiver));
            reportDto.setComment(commentDto);
        } else if ("reply".equals(kind)) {
            PReply pReply = pReplyDao.selectById(linkId);
            if(pReply==null){
                throw new ParamLossErrorException("linkid传递错误，不存在");
            }

            UUser receiver = uUserDao.selectById(pReply.getSenderId());
            PReplyDto pReplyDto = new PReplyDto();
            pReplyDto.setPReply(pReply);
            pReplyDto.setSenderUser(new UserInfo(receiver));
            pReplyDto.setReceiverUser(new UserInfo(uUserDao.selectById(pReply.getReceiverId())));
            reportDto.setReceiver(new UserInfo(receiver));
            reportDto.setReply(pReplyDto);
        } else {
            throw new ParamLossErrorException("kind传递值错误");
        }
        return reportDto;
    }
}
